<!--  -->
<template>
  <div>
    <van-nav-bar title="账号登陆" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.username"
        placeholder="请输入账号"
        :rules="unRules"
      />
      <van-field
        v-model="user.password"
        type="password"
        placeholder="请输入密码"
        :rules="pswRules"
      />
      <div style="margin: 16px">
        <van-button  block type="info" native-type="submit"
        color=#1cb676
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginApi } from '@/api'
export default {
  data () {
    return {
      user: {
        username: 'hzhmqd',
        password: '123456'
      },
      unRules: [{ required: true, message: () => { this.$toast('账号不能为空') } }],
      pswRules: [{ required: true, message: () => { this.$toast('密码不能为空') } }]
    }
  },
  beforeDestroy () {
    setTimeout(() => {
      this.$toast.clear()
    }, 500)
  },
  methods: {
    onClickLeft () {
      console.log(1)
    },
    async onSubmit () {
      try {
        const res = await loginApi(this.user)
        this.$toast(res.data.description)
        // console.log(res)
        // console.log(res.data.status)
        if (res.data.status === 200) {
          this.$store.commit('getToken', res.data.body)
          this.$router.push({
            name: 'my'
          })
          // console.log(this.$store.state.token)
        }
      } catch (error) {
        this.$toast(error.message)
        // console.log(error.message)
      }
    }
  }
}
</script>
<style lang='less' scoped>
</style>
